package cn.chinaflame.pagodas.repository;

import cn.chinaflame.pagodas.entity.PaAgent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


public interface PaAgentRepository extends JpaRepository<PaAgent, String>, JpaSpecificationExecutor<PaAgent> {

    @Query("select c from PaAgent c where c.mobile = ?1")
    public PaAgent findByMobile(String mobile);

    @Query("select c from PaAgent c where c.loginId = ?1")
    public PaAgent findByLoginId(String loginId);

    @Query("select c from PaAgent c where c.email = ?1")
    public PaAgent findByEmail(String email);

    @Query("select c from PaAgent c where c.mobile = ?1 and c.loginPwd = ?2")
    public PaAgent findByMobileAndPwd(String mobile, String loginPwd);

    @Query("select c from PaAgent c where c.loginId = ?1 and c.loginPwd = ?2")
    public PaAgent findByLoginIdAndPwd(String loginId, String loginPwd);

    @Query("select c from PaAgent c where c.email = ?1 and c.loginPwd = ?2")
    public PaAgent findByEmailAndPwd(String email, String loginPwd);

    @Query("select c from PaAgent c where c.agentLevel = ?1")
    public List<PaAgent> findByLevel(String agentLevel);

    @Query("select c from PaAgent c where c.parentId = ?1")
    public List<PaAgent> findByParentId(String parentId);

    @Query("select c from PaAgent c where c.parentId = ?1 and c.projectId = ?2")
    public List<PaAgent> findByLevelAndProjectId(String parentId, String projectId);
    
    @Modifying
    @Query("update PaAgent c set c.loginPwd = ?2 where c.id = ?1")
    public Integer updatePwdById(String id, String pwd);
}
